// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: qiang

#pragma once

#include <memory>
#include <string>

#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/bitget_base/api_util/auth_key.h"
#include "coin2/exchange/bitget_base/api_util/rate_tracker.h"
#include "coin2/exchange/order/bitget.pb.h"

namespace coin2::exchange::bitget_base::api_order {

enum BitgetOrderSubTopicId : int { REST_DEPOSIT = 10000, REST_WITHDRAWAL = 10001 };

}  // namespace coin2::exchange::bitget_base::api_order

class BitgetFuturesNativePrivateClient : public coin2::exchange::base::order::NativePrivateClient {
 public:
  using NativeClientConfig = coin2::exchange::order::OrderConnectionConfig;
  using AsioHttpContextList = std::list<std::shared_ptr<AsioHttpContext>>;
  using RestParams = coin2::exchange::order::BitgetFuturesRestParams;
  using BitgetRateTracker = coin2::exchange::bitget::api_util::BitgetRateTracker;

 public:
  AsioHttpContextList SubmitRestQueryList(const RestQueryProto& query);
  std::shared_ptr<AsioHttpContext> PlaceOrderNorm(const OrderExecution&);
  std::shared_ptr<AsioHttpContext> CancelOrderNorm(const OrderExecution&);
  std::shared_ptr<AsioHttpContext> QueryBalance(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryAccountPosition(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryOpenOrders(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryAllOrders(const RestParams& params);
  std::shared_ptr<AsioHttpContext> PlaceOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> CancelOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryFills(const RestParams& params);

  RestParams GetRestOrderParams(const OrderSpec& order_spec, int64_t proc_order_id);
  RestParams GetRestCancelParams(const IOrderContext& order_context);

 public:
  explicit BitgetFuturesNativePrivateClient(
      const MarketExchangeApi& mea,
      const NativeClientConfig& config);

 private:
  RoundRobinRestAuth order_auths_;
  bool is_first_query_account_balance_{true};
  bool is_first_query_postion_balance_{true};
  BitgetRateTracker* local_rate_tracker_;
};
